package com.zzyl.nursing.controller;

import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.nursing.domain.NursingTask;
import com.zzyl.nursing.dto.NursingTaskCancelDto;
import com.zzyl.nursing.dto.NursingTaskDto;
import com.zzyl.nursing.dto.NursingTaskExecuteDto;
import com.zzyl.nursing.dto.NursingTaskRescheduleDto;
import com.zzyl.nursing.service.INursingTaskService;
import com.zzyl.nursing.vo.NursingTaskVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 护理任务Controller
 *
 * @author ruoyi
 * @date 2024-09-27
 */
@RestController
@RequestMapping("/nursing/task")
@Api(tags = "护理任务的接口")
@Slf4j
public class NursingTaskController extends BaseController
{
    @Autowired
    private INursingTaskService nursingTaskService;

    /**
     * 查询护理任务列表
     */
    @ApiOperation("查询护理任务列表")
    @GetMapping("/list")
    public TableDataInfo<NursingTaskVo> list(NursingTaskDto nursingTaskDto)
    {
        startPage();

        return getDataTable(nursingTaskService.selectNursingList(nursingTaskDto));
    }

    /**
     * 导出护理任务列表
     */
    @ApiOperation("导出护理任务列表")
    @Log(title = "护理任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NursingTask nursingTask)
    {
        List<NursingTask> list = nursingTaskService.selectNursingTaskList(nursingTask);
        ExcelUtil<NursingTask> util = new ExcelUtil<NursingTask>(NursingTask.class);
        util.exportExcel(response, list, "护理任务数据");
    }

    /**
     * 获取护理任务详细信息
     */
    @ApiOperation("获取护理任务详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam(value = "护理任务ID", required = true)
                              @PathVariable("id") Long id)
    {
        // 调用新方法获取Vo，返回给前端（自动序列化为JSON，匹配响应示例格式）
        NursingTaskVo nursingTaskVo = nursingTaskService.selectNursingTaskVoById(id);
        // 若无数据返回404，有数据返回200+Vo
        return nursingTaskVo != null ? success(nursingTaskVo) : AjaxResult.error("护理任务不存在");
//        return success(nursingTaskService.selectNursingTaskById(id));
    }

    /**
     * 新增护理任务
     */
    @ApiOperation("新增护理任务")
    @Log(title = "护理任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam(value = "护理任务实体")
                          @RequestBody NursingTask nursingTask)
    {
        return toAjax(nursingTaskService.insertNursingTask(nursingTask));
    }

    /**
     * 修改护理任务
     */
    @ApiOperation("修改护理任务")
    @Log(title = "护理任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@ApiParam(value = "护理任务实体")
                           @RequestBody NursingTask nursingTask)
    {
        return toAjax(nursingTaskService.updateNursingTask(nursingTask));
    }

    /**
     * 删除护理任务
     */
    @ApiOperation("删除护理任务")
    @Log(title = "护理任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(nursingTaskService.deleteNursingTaskByIds(ids));
    }

    /**
     * 取消护理任务
     * @param dto 取消任务的请求参数
     * @return 操作结果
     */
    @ApiOperation("取消护理任务")
    @PutMapping("/cancel") // 匹配接口文档的PUT请求和地址
    public AjaxResult cancelTask(@ApiParam(value = "取消护理任务参数", required = true)
                                 @RequestBody NursingTaskCancelDto dto) {
        NursingTaskVo taskVo = nursingTaskService.cancelTask(dto);
        return AjaxResult.success("任务取消成功", taskVo);
    }

    /**
     * 执行护理任务
     * @param dto 执行任务的请求参数
     * @return 操作结果
     */
    @ApiOperation("执行护理任务")
    @PutMapping("/do")
    public AjaxResult executeTask(@ApiParam(value = "执行护理任务参数", required = true)
                                  @RequestBody NursingTaskExecuteDto dto) {
        NursingTaskVo taskVo = nursingTaskService.executeTask(dto);
        log.info("执行任务后返回前端的Vo数据：{}", taskVo);
        return AjaxResult.success("任务执行成功", taskVo);
    }
    /**
     * 任务改期
     * @param dto 改期请求参数
     * @return 操作结果
     */
    @ApiOperation("任务改期")
    @PutMapping("/updateTime")
    public AjaxResult rescheduleTask(@ApiParam(value = "任务改期参数", required = true)
                                     @RequestBody NursingTaskRescheduleDto dto) {
        nursingTaskService.rescheduleTask(dto);
        return AjaxResult.success("任务改期成功");
    }
}
